# @date: 2024/07/19 17:06
# @author: ls
# @describe: 虹软人脸-测试demo
import cv2
from arcface.engine import *

APPID = b'xxxxxx'
SDKKey = b'xxxxxx'

# 在线激活SDK
res = ASFOnlineActivation(APPID, SDKKey)
if (MOK != res and MERR_ASF_ALREADY_ACTIVATED != res):
    print(f"在线激活失败,状态码:{res}")
else:
    print(f"在线激活成功或已激活(90114),状态码:{res}")

# 获取激活文件信息
res, activeFileInfo = ASFGetActiveFileInfo()
if (res != MOK):
    print("获取激活文件信息失败: {}".format(res))
else:
    print(f'激活文件信息:SDK开始时间:{activeFileInfo.startTime}')
    print(f'激活文件信息:SDK截止时间:{activeFileInfo.endTime}')
    print(f'激活文件信息:激活码:{activeFileInfo.activeKey}')
    print(f'激活文件信息:平台版本:{activeFileInfo.platform}')
    print(f'激活文件信息:SDK 类型:{activeFileInfo.sdkType}')
    print(f'激活文件信息:APPID:{activeFileInfo.appId}')
    print(f'激活文件信息:SDKKEY:{activeFileInfo.sdkKey}')
    print(f'激活文件信息:SDK 版本号:{activeFileInfo.sdkVersion}')
    print(f'激活文件信息:激活文件版本号:{activeFileInfo.fileVersion}')

# 获取人脸识别引擎
face_engine = ArcFace()

# 需要引擎开启的功能
mask = ASF_FACE_DETECT | ASF_FACERECOGNITION | ASF_AGE | ASF_GENDER | ASF_LIVENESS | ASF_IR_LIVENESS

# 初始化接口
res = face_engine.ASFInitEngine(ASF_DETECT_MODE_IMAGE, ASF_OrientCode.ASF_OC_0.value, 30, 50, mask)
if (res != MOK):
    print("引擎初始化失败,状态码: {}".format(res))
else:
    print("引擎初始化成功,状态码: {}".format(res))

# # RGB图像
img1 = cv2.imread("D:\\test\\test_img\\111.jpg")
img2 = cv2.imread("D:\\test\\test_img\\222.jpg")

# 图片宽度必须为4的倍数
sp1 = img1.shape
img1 = cv2.resize(img1, (sp1[1] // 4 * 4, sp1[0]))
sp2 = img2.shape
img2 = cv2.resize(img2, (sp2[1] // 4 * 4, sp2[0]))

face_feature1 = None
face_feature2 = None

# 检测第一张图中的人脸
res, detectedFaces1 = face_engine.ASFDetectFaces(img1)
if res == MOK:
    print(f"人脸1检测成功,状态码:{res},检测到{detectedFaces1.faceNum}个人")
    if detectedFaces1.faceNum > 0:
        single_detected_face1 = ASF_SingleFaceInfo()
        single_detected_face1.faceRect = detectedFaces1.faceRect[0]
        single_detected_face1.faceOrient = detectedFaces1.faceOrient[0]
        # 特征值提取
        res, face_feature1 = face_engine.ASFFaceFeatureExtract(img1, single_detected_face1)
        if (res != MOK):
            print(f"人脸1特征提取失败,状态码:{res}")
        else:
            print(f"人脸1特征提取成功,状态码:{res}")
else:
    print(f"人脸1检测失败,状态码:{res}")

# 检测第二张图中的人脸
res, detectedFaces2 = face_engine.ASFDetectFaces(img2)
if res == MOK:
    print(f"人脸2检测成功,状态码:{res},检测到{detectedFaces2.faceNum}个人")
    if detectedFaces2.faceNum > 0:
        single_detected_face2 = ASF_SingleFaceInfo()
        single_detected_face2.faceRect = detectedFaces2.faceRect[0]
        single_detected_face2.faceOrient = detectedFaces2.faceOrient[0]
        # 特征值提取
        res, face_feature2 = face_engine.ASFFaceFeatureExtract(img2, single_detected_face2)
        if (res != MOK):
            print(f"人脸2特征提取失败,状态码:{res}")
        else:
            print(f"人脸2特征提取成功,状态码:{res}")
else:
    print(f"人脸2检测失败,状态码:{res}")

# 比较两个人脸的相似度
if face_feature1 and face_feature2:
    res, score = face_engine.ASFFaceFeatureCompare(face_feature1, face_feature2)
    if (res != MOK):
        print(f"人脸对比失败,状态码:{res}")
    else:
        print(f"人脸对比成功,相似度:{score}")

# 人脸属性检测（年龄/性别/口罩），最多支持4张人脸信息检测，超过部分返回未知（活体仅支持单张人脸检测，超出返回未知）,接口不支持IR图像检测。
processMask = ASF_AGE | ASF_GENDER | ASF_LIVENESS

res = face_engine.ASFProcess(img1, detectedFaces1, processMask)

if res == MOK:
    print(f"人脸属性检测成功,状态码:{res}")
    # 获取年龄
    res, ageInfo = face_engine.ASFGetAge()
    if (res != MOK):
        print(f"获取年龄失败:状态码:{res}")
    else:
        print(f"获取年龄成功:检测人数:{ageInfo.num},第一个年龄:{ageInfo.ageArray[0]}")

    # 获取性别
    res, genderInfo = face_engine.ASFGetGender()
    if (res != MOK):
        print(f"获取性别失败:状态码: {res}")
    else:
        print(f"获取性别成功:检测人数:{genderInfo.num},第一个性别:{genderInfo.genderArray[0]}")

    # 获取RGB活体信息
    res, rgbLivenessInfo = face_engine.ASFGetLivenessScore()
    if (res != MOK):
        print(f"获取RGB活体信息失败:状态码: {res}")
    else:
        print(f"获取RGB活体信息成功:检测人数:{rgbLivenessInfo.num},第一个活体信息:{rgbLivenessInfo.isLive[0]}")
else:
    print(f"人脸属性检测失败,状态码:{res}")

# 销毁引擎
print("销毁引擎")
face_engine.ASFUninitEngine()
